var apiURL = '/api/Case';

$(document).ready(function () {
    var caseOwner = localStorage["loginType"];
    if(caseOwner === "PSD"){
        $('#CaseTable').dataTable({
        "bProcessing": false,
        "sAjaxSource": 'api/Case',
        "sAjaxDataProp": "",
        "bPaginate": true,
        "bLengthChange": false,
        "bFilter": false,
        "bSort": true,
        "aaSorting": [[7, 'asc']],
        "bInfo": false,
        "bAutoWidth": false,
        "aoColumnDefs": [
        {
            "bSortable": true,
            "aTargets": [0],
            "mData": "timesRouted",
            "mRender":
                function (data, type, full) {
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data > 3) {
                            return '<i class="fa fa-bell"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (data > 3) {
                            return '<i class="fa fa-bell"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "bSortable": true,
            "aTargets": [1],
            "mData": "dueDate",
            "mRender":
                function (data, type, full) {
                    var today = new Date();
                    var inputDate = new Date(data);
                    var diff = today - inputDate;
                    var days = diff / 1000 / 60 / 60 / 24;
                    console.log(days);
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (days > 3) {
                            return '<i class="fa fa-exclamation"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (days > 3) {
                            return '<i class="fa fa-exclamation"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "bSortable": true,
            "aTargets": [2],
            "mData": "jointID",
            "mRender":
                function (data, type, full) {
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data !== "") {
                            return '<i class="fa fa-group"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (data !== "") {
                            return '<i class="fa fa-group"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "aTargets": [3],
            "mData": "caseID",
            "mRender":
                function (data, type, full) {
                    var res = data.substring(0, 12);
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data !== "") {
                            return res;
                        }
                    }
                    else if (type === 'filter') {
                        if (data !== "") {
                            return res;
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                }
        },
            {
                "aTargets": [4],
                "mData": "subject"
            },
            {
                "aTargets": [5],
                "mData": "category"
            },
            {
                "aTargets": [6],
                "mData": "caseStatus"
            },
            {
                "aTargets": [7],
                "mData": "feedbackDate"
            },
            {
                "aTargets": [8],
                "mData": "dueDate"
            },
            {
                "aTargets": [9],
                "mData": "caseOwner"
            }
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            var caseID = aData.caseID;
            console.log(caseID);
            $(nRow).click(function () {
                if(aData.jointID!=="")
                {
                    localStorage["caseID"] = caseID;
                    document.location.href = "joint-resolution(1).html";
                }else{
                    localStorage["caseID"] = caseID;
                    document.location.href = "case-detail(1).html";
                }
            });
            
            $(nRow).hover(function () {
                $(this).css('cursor', 'pointer');
            }, function () {
                $(this).css('cursor', 'auto');
            });
        },
         "fnDrawCallback": function( oSettings ) {
          NProgress.start();
        },
        "fnInitComplete": function(oSettings, json) {
          NProgress.done();
        }
    });
    }else{
        $('#CaseTable').dataTable({
        "bProcessing": true,
        "sAjaxSource": 'api/Case/GetCasesByCaseOwner?caseOwner='+caseOwner,
        "sAjaxDataProp": "",
        "bPaginate": true,
        "bLengthChange": false,
        "bFilter": false,
        "bSort": true,
        "aaSorting": [[7, 'asc']],
        "bInfo": false,
        "bAutoWidth": false,
        "aoColumnDefs": [
        {
            "bSortable": true,
            "aTargets": [0],
            "mData": "timesRouted",
            "mRender":
                function (data, type, full) {
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data > 3) {
                            return '<i class="fa fa-bell"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (data > 3) {
                            return '<i class="fa fa-bell"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "bSortable": true,
            "aTargets": [1],
            "mData": "dueDate",
            "mRender":
                function (data, type, full) {
                    var today = new Date();
                    var inputDate = new Date(data);
                    var diff = today - inputDate;
                    var days = diff / 1000 / 60 / 60 / 24;
                    console.log(days);
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (days > 3) {
                            return '<i class="fa fa-exclamation"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (days > 3) {
                            return '<i class="fa fa-exclamation"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "bSortable": true,
            "aTargets": [2],
            "mData": "jointID",
            "mRender":
                function (data, type, full) {
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data !== "") {
                            return '<i class="fa fa-group"></i>';
                        }
                    }
                    else if (type === 'filter') {
                        if (data !== "") {
                            return '<i class="fa fa-group"></i>';
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                        
                }
        },
        {
            "aTargets": [3],
            "mData": "caseID",
            "mRender":
                function (data, type, full) {
                    var res = data.substring(0, 12);
                    if (type === 'set') {
                        return;
                    }
                    else if (type === 'display') {
                        if (data !== "") {
                            return res;
                        }
                    }
                    else if (type === 'filter') {
                        if (data !== "") {
                            return res;
                        }
                    }
                    // 'sort', 'type' and undefined all just use the integer
                    return '';
                }
        },
            {
                "aTargets": [4],
                "mData": "subject"
            },
            {
                "aTargets": [5],
                "mData": "category"
            },
            {
                "aTargets": [6],
                "mData": "caseStatus"
            },
            {
                "aTargets": [7],
                "mData": "feedbackDate"
            },
            {
                "aTargets": [8],
                "mData": "dueDate"
            },
            {
                "aTargets": [9],
                "mData": "caseOwner"
            }
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            var caseID = aData.caseID;
            console.log(caseID);
            $(nRow).click(function () {
                if(aData.jointID!=="")
                {
                    localStorage["caseID"] = caseID;
                    document.location.href = "joint-resolution(1).html";
                }else{
                    localStorage["caseID"] = caseID;
                    document.location.href = "case-detail(1).html";
                }
            });
            
            $(nRow).hover(function () {
                $(this).css('cursor', 'pointer');
            }, function () {
                $(this).css('cursor', 'auto');
            });
        },
        "fnDrawCallback": function( oSettings ) {
          NProgress.start();
        },
        "fnInitComplete": function(oSettings, json) {
            NProgress.done();
        }
    });
    }




$('#linkCaseTable').dataTable({
        "bLengthChange": false,
        "bFilter": false,
        "bSort": true,
        "aaSorting": [[5, 'asc']],
        "bInfo": false,
        "bAutoWidth": false,
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
            var caseID = aData.caseID;
            //console.log(caseID);
            $('td:eq(1)',nRow).click(function () {
                localStorage["caseID"] = caseID;
                document.location.href = "case-detail(1).html";
            });
            
            $(nRow).hover(function () {
                $(this).css('cursor', 'pointer');
            }, function () {
                $(this).css('cursor', 'auto');
            });
        }
    });
    $('#cbxAll').click(function(event) {
        var selected = this.checked;
        $('input[name="link-cbx"').each(function () {
            this.checked = selected;
        });
    });


});
